package Lesson8;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * Created by trimmm on 24.02.15.
 */
public class SecondTC {
    public static void main(String[] args) throws Exception{
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS); //default setting for waiting all web elements

        driver.get("https://ankiweb.net");

        WebElement fieldEmail = driver.findElement(By.xpath(".//*[@id='email']"));
        fieldEmail.clear();
        fieldEmail.sendKeys("selenium-2@mail.ua");

        WebElement fieldPassword = driver.findElement(By.xpath(".//*[@id='pass']"));
        fieldPassword.clear();
        fieldPassword.sendKeys("qwe123456");

        WebElement buttonLogin = driver.findElement(By.xpath(".//*[@value='Login']"));
        buttonLogin.click();

        // perform login to the site. If failed, not continue to perform test
        if (!driver.getCurrentUrl().equals("https://ankiweb.net/study/")){
            System.out.println("Test failed");
            throw new Exception();
        }

        //first check availability of deck "testDeck"
        //if it exist, look and remember cards quantyty
        driver.findElement(By.xpath(".//*[@id='header']//a[contains(text(),'Decks')]")).click();

        if (!driver.getCurrentUrl().equals("https://ankiweb.net/decks/")){
            System.out.println("Test failed");
            throw new Exception();
        }

        int qwtNewCardsBefore = 0;

        try {
            WebElement cell = driver.findElement(By.xpath("//a[@full = 'testDeck']/../../td[3]"));
            qwtNewCardsBefore = Integer.parseInt(cell.getText());
        }catch (NumberFormatException e){
            qwtNewCardsBefore = 0;
        }catch (Exception e){
            qwtNewCardsBefore = 0;
        }

        // Add new card to test deck
        driver.findElement(By.xpath(".//*[@id='header']//a[contains(text(),'Add')]")).click();
        if (!driver.getCurrentUrl().equals("https://ankiweb.net/edit/")){
            System.out.println("Test failed");
            throw new Exception();
        }

        // set deck name in which new cards will be added
        WebElement deck = driver.findElement(By.xpath(".//input[@id='deck']"));

        deck.clear();
        deck.sendKeys("testDeck");

        // set cards front name to current time
        WebElement fieldFront = driver.findElement(By.xpath(".//div[@id='fields']//table//tr[1]//div"));

        fieldFront.clear();
        fieldFront.sendKeys(""+System.currentTimeMillis());

        // set cards back name to incremented qwtNewCards
        WebElement fieldBack = driver.findElement(By.xpath(".//div[@id='fields']//table//tr[2]//div"));

        fieldBack.clear();
        fieldBack.sendKeys("New card №"+qwtNewCardsBefore);

        driver.findElement(By.xpath(".//table//button[contains(text(), 'Save')]")).click();

        // look and compare new cards quantyty
        driver.findElement(By.xpath(".//*[@id='header']//a[contains(text(),'Decks')]")).click();

        if (!driver.getCurrentUrl().equals("https://ankiweb.net/decks/")){
            System.out.println("Test failed");
            throw new Exception();
        }

        int qwtNewCardsAfter = 0;

        try {
            WebElement cell = driver.findElement(By.xpath("//a[@full = 'testDeck']/../../td[3]"));
            qwtNewCardsAfter = Integer.parseInt(cell.getText());
        }catch (NumberFormatException e){
            qwtNewCardsAfter = 0;
        }catch (Exception e){
            qwtNewCardsAfter = 0;
        }

        if (qwtNewCardsAfter == (qwtNewCardsBefore + 1)){
            System.out.println("Test pass");
        }else {
            System.out.println("Test failed");
        }
        driver.quit();
    }

}
